assume cs:codeseg, ds:dataseg, ss:stackseg  ; 修正逗号分隔

dataseg segment
mus_freq dw 262,262,262,196
         dw 330,330,330,262
         dw 262,330,392,392
         dw 349,330,294
         dw 294,330,349,349
         dw 330,294,330,262
         dw 262,330,294,196
         dw 247,294,262,-1

mus_time dw 3 dup(12,12,25,25),12,12,50
         dw 3 dup(12,12,25,25),12,12,50
dataseg ends

stackseg segment
    db 100h dup(0)
stackseg ends

codeseg segment
start:
    mov ax, stackseg
    mov ss, ax
    mov sp, 100h
    mov ax, dataseg
    mov ds, ax
    lea si, mus_freq
    lea di, mus_time

play:
    mov dx, [si]
    cmp dx, -1
    je end_play
    call sound
    add si, 2
    add di, 2
    jmp play

end_play:
    mov ax, 4c00h
    int 21h

sound:
    push ax
    push dx
    push cx

    ; 设置定时器
    mov al, 0B6h
    out 43h, al
    mov dx, 12h
    mov ax, 34DCh
    div word ptr [si]
    out 42h, al
    mov al, ah
    out 42h, al

    ; 开启扬声器
    in al, 61h
    or al, 03h
    out 61h, al

    ; 调整后的延时循环
    mov dx, [di]
wait1:
    mov cx, 0FFFFh  ; 增加内层循环次数
delay:
    nop
    loop delay
    dec dx
    jnz wait1

    ; 关闭扬声器
    in al, 61h
    and al, 0FCh
    out 61h, al

    pop cx
    pop dx
    pop ax
    ret

codeseg ends
end start